package com.kedou.sdk.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.kedou.sdk.params.RedpackageResponseParams;
import com.kedou.sdk.params.bean.RedpackageHistory;
import com.kedou.sdk.ui.view.ListViewForScrollView;
import com.kedou.sdk.utils.ResUtils;

import java.util.List;

public class RedPackageDetailFragment extends Fragment  implements View.OnClickListener {

//    private EditText mIdentificationNameEt;
//    private EditText mIdentificationCardEt;
//
//    private Button mBindPhoneBtn;


//    public static RedPackageFragment newInstance(Bundle bundle) {
//        RedPackageFragment fragment = new RedPackageFragment();
//        fragment.setArguments(bundle);
//        return fragment;
//    }

    private ChangeFragmentLister changeFragmentLister;
    private ImageView backImage;
    private ListViewForScrollView detailListView;



    public RedPackageDetailFragment(ChangeFragmentLister changeFragment){
        this.changeFragmentLister = changeFragment;
    }

    private RedpackageResponseParams.RedpackageResponseData data ;
    public void updateData(RedpackageResponseParams.RedpackageResponseData data ){
        this.data = data;
    }

    private List<RedpackageHistory> list;

    private void setData(){
        if(data==null)return;
        this.list = data.getHongbao_log();
        detailListView.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return list==null?0:list.size();
            }

            @Override
            public Object getItem(int position) {
                return list.get(position);
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                ViewHolder viewHolder;
                if (convertView == null) {
                    viewHolder = new ViewHolder();
                    final Context context = parent.getContext();
                    LayoutInflater inflater = LayoutInflater.from(context);
                    final int layoutResId= ResUtils.getLayoutIdentifier(context, "hnssn_item_redpackage_history");
                    final int titleResId = ResUtils.getViewIdentifier(context, "redpackage_history_title");
                    final int moneyResIds = ResUtils.getViewIdentifier(context, "redpackage_history_money");
                    final int timeResId = ResUtils.getViewIdentifier(context, "redpackage_history_time");
                    final int yue = ResUtils.getViewIdentifier(context, "redpackage_history_yue");
                    convertView = inflater.inflate(layoutResId, parent, false);
                    viewHolder.title = convertView.findViewById(titleResId);
                    viewHolder.money = convertView.findViewById(moneyResIds);
                    viewHolder.time = convertView.findViewById(timeResId);
                    viewHolder.yue = convertView.findViewById(yue);

                    convertView.setTag(viewHolder);
                }else{
                    viewHolder = (ViewHolder) convertView.getTag();
                }
                RedpackageHistory history = list.get(position);
                viewHolder.title.setText(history.getTitle());
                viewHolder.money.setText((history.getType()==1?"+":"-")+history.getMoney());
                viewHolder.time.setText(history.getCreated_at());
                viewHolder.yue.setText("余额："+history.getAfter_balance());

                return convertView;
            }
        });

    }

    public static class ViewHolder {
        public TextView title;
        public TextView money;
        public TextView time;
        public TextView yue;
    }

    @Override
    public void onResume() {
        super.onResume();
        setData();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        int resource = ResUtils.getLayoutIdentifier(getActivity(), "hnssn_fragment_redpackage_detail");
        View contentView = inflater.inflate(resource, null);
        setView(contentView);

        return contentView;
    }

    private void setView(View view){
        view.setOnClickListener(this);
        backImage = view.findViewById(ResUtils.getViewIdentifier(getActivity(), "fragment_redpackagedetail_back"));
        backImage.setOnClickListener(this);
        detailListView = view.findViewById(ResUtils.getViewIdentifier(getActivity(), "repackage_task_list"));

    }

    @Override
    public void onClick(View v) {
        if(v == backImage){
            changeFragmentLister.changeToMain();
        }
    }
}
